home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / rexx / 402 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.8 KB  |  135 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: 73021.1122@compuserve.com (Lee Hayden)
  3. Newsgroups: comp.lang.rexx
  4. Subject: Re: string substitution in (standard) REXX
  5. Date: Sat, 20 Jan 1996 15:20:00 GMT
  6. Organization: CompuServe Incorporated
  7. Distribution: inet
  8. Message-ID: <4dr1a6$h4c@dub-news-svc-4.compuserve.com>
  9. References: <4d2mp2$1up@hyperion.mfltd.co.uk> <4d2phs$kr2@news.be.innet.net>
  10. NNTP-Posting-Host: dd33-138.compuserve.com
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. michelc@jeeves.be (Michel Castelein) wrote:
  14.  
  15. >Derek Morris <dom@mfltd.co.uk> wrote:
  16.  
  17. >>Hi
  18.  
  19. >>I want to find a QUICK way of doing a general purpose string substitution
  20. >>in 'standard' REXX (i.e not using a prticular vendors extensions).
  21. >>For example, I want to replace all 'foo' with 'bar' in every line in a file.
  22. >>I have tried OVERLAY having found the substring, but that is quite slow.
  23. >>Any better suggestions? Thanks
  24. >>Derek Morris
  25.  
  26. >Your REXX exec  can invoke the 'standard' editor (e.g. XEDIT under VM,
  27. >or the ISFP editor) to execute an editor command such as 'CHANGE FOO
  28. >BAR ALL).
  29. >  
  30. >Michel                     _\|/_
  31. >                           >0 0<
  32. >------------------------oOo-(_)-oOo-------------------------
  33. >| Michel Castelein                                         |
  34. >| Education Consultant (MVS, OS/390)                       |
  35. >| E-mail michelc@jeeves.be                                 |
  36. >| Home Page: http://www.club.innet.be/~pub00543/           |
  37. >|                                                          |
  38. >| Company:   JEEVES CONSULTING N.V.                        |
  39. >|            Bedrijvencentrum                              |
  40. >|            Mechelsesteenweg 277                          |
  41. >|            B - 1800 Vilvoorde (Belgium)                  |
  42. >| Telephone: +32-2-2516650                                 |
  43. >|       Fax: +32-2-2525755                                 |
  44. >------------------------------------------------------------
  45.  
  46. Michel,
  47.  
  48. Here are a couple of GP string substitution routines. Hope the are
  49. what you are looking for.
  50.  
  51. Lee Hayden
  52. DB2 Sysprog
  53.  
  54. /* REXX */
  55.  
  56.   string = 'The quick brown fox jumped over the lazy dog.'
  57.   new_string = change_string(string,'dog.','cat,')
  58.   say 'old string=>'string'<'
  59.   say 'new string=>'new_string'<'
  60.   exit
  61.  
  62.  
  63.   Change_string: Procedure      /* function to change all         */
  64.                                 /* occurances of a substring      */
  65.                                 /* found in a larger string       */
  66.                                 /* to another supplied value.     */
  67.  
  68.   parse arg string,from_s,to_s  /* preserve case in parms         */
  69.   len_from=length(from_s)       /* save lenght for later process  */
  70.   len_to=length(to_s)
  71.   from_pos=1                    /* initial position to begin scan */
  72.                                 /* this gets incremented as we    */
  73.                                 /* get hits to avoid problem with */
  74.                                 /* replacements strings that are  */
  75.                                 /* supersets of the original.     */
  76.  
  77.   do while pos(from_s,string,from_pos)>0
  78.      from_pos=pos(from_s,string,from_pos)        /* update latest pos
  79. */
  80.      string=delstr(string,from_pos,len_from)     /* delete "from"
  81. */
  82.      string=insert(to_s,string,from_pos-1)       /* insert "to"
  83. */
  84.      from_pos=from_pos+len_to                    /* bump past change
  85. */
  86.   end
  87.  
  88.   return string
  89.  
  90.  /* REXX */
  91.  
  92.   string = 'The quick brown fox jumped over the lazy dog.'
  93.   new_string = change_string(string,'dog.','cat,')
  94.   say 'old string=>'string'<'
  95.   say 'new string=>'new_string'<'
  96.   exit
  97.  
  98.   Change_string: Procedure      /* function to change all         */
  99.                                 /* occurances of a substring      */
  100.                                 /* found in a larger string       */
  101.                                 /* to another supplied value.     */
  102.  
  103.   parse arg string,from_s,to_s  /* preserve case in parms         */
  104.  
  105.   whats_left=string             /* set initial value              */
  106.  
  107.   if pos(from_s,whats_left)>0 then          /* found at least one */
  108.      do
  109.        k=0                      /* index for changed "chunks"     */
  110.        do until pos(from_s,whats_left)=0               /* no more */
  111.  
  112.          /* split string using the "old" string as the parse
  113.              delimiter. Then put the string back together with
  114.              the "new" string in its place. Save this fron "chunk"
  115.              in a separate array of changed "chunks" to prevent
  116.              loop in case where new string is superset of old.
  117.          */
  118.  
  119.          parse var whats_left first_part (from_s) whats_left
  120.          k=k+1
  121.          changed.k=first_part]]to_s           /* make change      */
  122.        end
  123.  
  124.        temp=''                    /* concatenate changed "chunks" */
  125.        do l=1 to k
  126.          temp=temp]]changed.l
  127.        end
  128.        temp=temp]]whats_left      /* add whats left */
  129.  
  130.      end   /* "found" block if/do  */
  131.  
  132.   return temp
  133. 
  134.  
  135.